package com.example.broadcast;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.broadcast.receiver.OrderBroadcastReceiverA;
import com.example.broadcast.receiver.OrderBroadcastReceiverB;

public class OrderBroadcastActivity extends AppCompatActivity implements View.OnClickListener {
    public static final String ACTION_ORDER="com.example.chapter09.order";
    private OrderBroadcastReceiverA orderA;
    private OrderBroadcastReceiverB orderB;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_broadcast);
        findViewById(R.id.btn_order_broadcast).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent=new Intent(ACTION_ORDER);
        //有些广播需要权限才能收到，这个所需的权限就是在这里声明的
        sendOrderedBroadcast(intent,null);//发送有序广播
    }

    @Override
    protected void onStart() {
        super.onStart();
        orderA = new OrderBroadcastReceiverA();
        IntentFilter filterA=new IntentFilter(ACTION_ORDER);
        filterA.setPriority(8);
        registerReceiver(orderA,filterA);

        orderB = new OrderBroadcastReceiverB();
        IntentFilter filterB=new IntentFilter(ACTION_ORDER);
        filterB.setPriority(10);
        registerReceiver(orderB,filterB);
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(orderA);
        unregisterReceiver(orderB);
    }
}
